package com.server.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.service.impl.CaseStudyService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

@Controller
@RequestMapping("case/study")
public class CaseStudyController {
	
	@Autowired
	private CaseStudyService caseStudyService;

	@RequestMapping("")
	public ModelAndView study(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("study/caseStudy/casePage");
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey);
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		mv.addObject("userName", userName);
		return mv;
	}
	@RequestMapping("list")
	public void list(HttpServletRequest request,HttpServletResponse response){
		String whereStr = "state=5";
		String sortStr = "time DESC";
		DataVector<Map<String, Object>> listData = caseStudyService.getList(whereStr, sortStr, new RequestInput(request));
		List<Map<String, Object>> list = listData.getList();
		Iterator<Map<String, Object>> iterator = list.iterator();
		while(iterator.hasNext()){
			Map<String, Object> row = iterator.next();
			row.put("handle_pro","");
			row.put("event_occurs","");
		}
		
		OutputUtil.returnGridData(response, listData.getCount(), JSONSerielizer.toJson(list, null, null));
	}
	@RequestMapping("detail/{id}")
	public ModelAndView getDetail(HttpServletRequest request,HttpServletResponse response,
			@PathVariable Integer id){
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey);
		Map<String, Object> returnMap = caseStudyService.caseDetail(id);
		Map<String, Object> scoreMap = caseStudyService.getScore(id.toString());
		ModelAndView mv = new ModelAndView("study/caseStudy/case_details");
		mv.addObject("caseMap", returnMap);
		mv.addObject("score",scoreMap);
		mv.addObject("userName", userName);
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		return mv;
	}
	@RequestMapping("score/{id}")
	public void score(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id){
		Map<String, Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> returnMap = new HashMap<String, Object>();
		String nickname = SessionUtil.getStringAttri(request, SessionData.nameKey);
		Boolean isSuccess = true;
		String info = "";
		if(StringUtils.isEmpty(nickname)){
			isSuccess = false;
			info = "请登录";
		}else{
			String utility = request.getParameter("utility");
			String clarity = request.getParameter("clarity");
			String heuristic = request.getParameter("heuristic");
			inputMap.put("utility", utility);
			inputMap.put("clarity", clarity);
			inputMap.put("heuristic", heuristic);
			inputMap.put("caseId", id);
			isSuccess = caseStudyService.score(nickname, inputMap);
			if(isSuccess){
				info="评分成功";
			}else{
				info="评分失败";
			}
		}
		returnMap.put("isSuccess", isSuccess);
		returnMap.put("info", info);
		OutputUtil.returnString(response, JSONSerielizer.toJson(returnMap, null, null));
	}
}
